package com.example.wishstar;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;

import com.example.wishstar.api.Api;
import com.example.wishstar.api.RetrofitClient;
import com.example.wishstar.db.UserDatabase;

public class MyApplication extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();

        userDatabase = UserDatabase.getInstance(this);
        api = RetrofitClient.getInstance().getApi();
    }

    private static UserDatabase userDatabase;
    private static Api api;

    public static Api getApi()
    {
        return api;
    }

    public static UserDatabase getUserDatabase()
    {
        return userDatabase;
    }

    public static int userId = 0;

    public static Activity findActivity(Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        }
        if (context instanceof ContextWrapper) {
            ContextWrapper wrapper = (ContextWrapper) context;
            return findActivity(wrapper.getBaseContext());
        } else {
            return null;
        }
    }
}
